package com.zxx.mvpPractice.utils;

import android.graphics.Bitmap;
import android.util.Log;

import com.zxx.mvpPractice.model.bean.RequestSmsBody;
import com.zxx.mvpPractice.model.net.RetrofitHelper;
import com.zxx.mvpPractice.model.net.bean.SmsResponse;
import com.zxx.mvpPractice.utils.callback.RxCallback;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * RxJava封装
 * Created by Administrator on 2017/1/10 0010.
 */

public class RxManagerUtils<T> {

    private static RxManagerUtils rxManagerUtils;

    public static RxManagerUtils getInstance() {
        if (rxManagerUtils == null) {
            rxManagerUtils = new RxManagerUtils();
        }
        return rxManagerUtils;
    }

    //Observer 即观察者，它决定事件触发的时候将有怎样的行为
    Observer<String> observer = new Observer<String>() {
        @Override
        public void onNext(String s) {
            KL.d(RxManagerUtils.class, "Item: " + s);
        }

        @Override
        public void onCompleted() {
            KL.d(RxManagerUtils.class, "Completed!");
        }

        @Override
        public void onError(Throwable e) {
            KL.d(RxManagerUtils.class, "Error!");
        }
    };

    //观察者Subscriber 对 Observer 接口进行了一些扩展，但他们的基本使用方式是完全一样的
    Subscriber<String> subscriber = new Subscriber<String>() {
        @Override
        public void onNext(String s) {
            KL.d(RxManagerUtils.class, "Item: " + s);
        }

        @Override
        public void onCompleted() {
            KL.d(RxManagerUtils.class, "Completed!");
        }

        @Override
        public void onError(Throwable e) {
            KL.d(RxManagerUtils.class, "Error!");
        }
    };

    //Observable 即被观察者，它决定什么时候触发事件以及触发怎样的事件
    Observable observable = Observable.create(new Observable.OnSubscribe<String>() {
        @Override
        public void call(Subscriber<? super String> subscriber) {
            subscriber.onNext("Hello");
            subscriber.onNext("Hi");
            subscriber.onNext("Aloha");
            subscriber.onCompleted();
        }
    });

    Observable observableJust = Observable.just("Hello", "Hi", "Aloha");

    String[] words = {"Hello", "Hi", "Aloha"};
    Observable observableArray = Observable.from(words);


    public void Test() {
        observable.subscribe(observer);

        observable.subscribe(subscriber);
    }

    /**
     * 订阅者io线程观察者主线程
     *
     * @param observable
     * @param rxCallback
     */
    public void commSubscribe(Observable<T> observable, final RxCallback<T> rxCallback) {
        observable.
                subscribeOn(Schedulers.io()).
                unsubscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<T>() {

            @Override
            public void onStart() {
                if (rxCallback != null) {
                    rxCallback.onStart();
                }
            }

            @Override
            public void onCompleted() {
                if (rxCallback != null) {
                    rxCallback.onCompleted();
                }
            }

            @Override
            public void onError(Throwable e) {
                Log.d("zxx", "showErrorMessage:" + e.getMessage());
                if (rxCallback != null) {
                    rxCallback.onError(e);
                }
            }

            @Override
            public void onNext(T response) {
                Log.d("zxx", "onNext:" + response.toString());
                if (rxCallback != null) {
                    rxCallback.onNext(response);
                }
            }
        });
    }


    /**
     * 变换
     * 所谓变换，就是将事件序列中的对象或整个序列进行加工处理，转换成不同的事件或事件序列
     */
//    public void rxForMap() {
//        Observable.just("images/logo.png")// 输入类型 String
//                .subscribeOn(Schedulers.computation())
//                .unsubscribeOn(Schedulers.computation())
//                .observeOn(AndroidSchedulers.mainThread())
//                .map(new Func1<String, Bitmap>() {
//                    @Override
//                    public Bitmap call(String filePath) {
//                        return getBitmapFromPath(filePath); // 返回类型 Bitmap
//                    }
//                })
//                .subscribe(new Action1<Bitmap>() {
//                    @Override
//                    public void call(Bitmap bitmap) {// 参数类型 Bitmap
//                        showBitmap(bitmap);
//                    }
//                });
//    }
//
//    public void rxForFlatMap() {
//        Student[] students = null;
//
//    }


}
